# App Settings & Materials (tutorials and intro to the app)
osSystem <- Sys.info()["sysname"]

materials <- sidebarLayout(position = "left",
                           sidebarPanel(
                             tags$head(
                               tags$script(src = paste0('http://cdn.mathjax.org/',
                                                        'mathjax/latest/MathJax.js',
                                                        '?config=TeX-AMS-MML_HTMLorMML'),
                                           type = 'text/javascript'),
                               includeScript("ui_layers/style/google-analytics.js")
                             ),
                             titlePanel("NicheToolBox"),
                             #h3('Welcome to NTB'),
                             br(),
                             p(paste0("This application has some tools",
                                      " to analyze and visualize the niche. ",
                                      "In this page you will find the",
                                      " settings of the app.")),
                             h4("Workflow"),
                             HTML(paste('Select a folder to save the files and data',
                                        "generated in NicheToolBox. Once you have" ,
                                        "specified the workflow directory, just press the",
                                        strong("Save state"), "button located the top-left",
                                        "corner of the application.")),
                             br(),br(),
                             conditionalPanel(paste0("'",osSystem,"' == 'Windows'"),
                                              directoryInput('wf_directory',
                                                             label = 'Select workflow directory')),
                             conditionalPanel(paste0("'",osSystem,"' != 'Windows'"),
                                              shinyFiles::shinyDirButton(id = "wf_directory",
                                                                         label =  "Select workflow directory",
                                                                         title = "Select a directory from the
                                                                         directories panel",
                                                                         icon = icon("folder-open",
                                                                                     lib = "glyphicon"),
                                                                         buttonType = "info"),
                                              br(),
                                              HTML(paste('<div class="alert alert-info" role="alert">',
                                                         verbatimTextOutput("work_directory"),'</div>'))

                             ),

                             #h3('Settings'),
                             br(),
                             h4("Niche layers"),
                             p(paste0("One ne of the most important ",
                                      "things for your data analysis",
                                      " in NTB is to select the folder",
                                      " with the raster layers that you ",
                                      "will use as niche variables.")),
                             br(),
                             p("Please select the folder of raster",
                               " layers; notice that all of them",
                               " need to be in the same resolution",
                               " and extension."),
                             p("The supported raster formats are: ",".asc",
                               ".bil",",",".tif",",",".nc.",
                               ",",".sdat",",",".img"),
                             br(),
                             #directoryInput('ras_layers_directory', label = 'Select a raster layers directory'),
                             #glyphicon glyphicon-folder-open
                             conditionalPanel(paste0("'",osSystem,"' == 'Windows'"),
                                              directoryInput('ras_layers_directory',
                                                             label = paste('Select a raster',
                                                                       ' layers directory')),
                                              br(),
                                              checkboxInput("load_projLayers_win",
                                                            label = "Select projection layers",
                                                            value = FALSE),
                                              conditionalPanel("input.load_projLayers_win == true",
                                                               h4("Projection layers"),
                                                               paste0("These are the layers",
                                                                 " of environmental change",
                                                                 " scenario. Please make sure",
                                                                 " that their names are the",
                                                                 " same as the niche layers."),
                                                               br(),
                                                               directoryInput('proj_layers_directory',
                                                                              label = paste0('Select',
                                                                                        ' projection',
                                                                                        ' layers',
                                                                                        ' directory')))
                                              ),
                             conditionalPanel(paste0("'",osSystem,"' != 'Windows'"),
                                              shinyFiles::shinyDirButton(id = "ras_layers_directory",
                                                                         label =  paste0("Select raster",
                                                                                         " layers directory"),
                                                                         title = paste0("Select a directory",
                                                                                        " from ",
                                                                                   "the directories panel"),
                                                                         icon = icon("folder-open",
                                                                                     lib = "glyphicon"),
                                                                         buttonType = "info"),
                                              br(),
                                              HTML(paste('<div class="alert alert-info" role="alert">',
                                                         verbatimTextOutput("layers_directory"),'</div>')),
                                              br(),br(),
                                              checkboxInput("load_projLayers",
                                                            label = "Select projection layers",
                                                            value = FALSE),
                                              conditionalPanel("input.load_projLayers == true",
                                                               h4("Projection layers"),
                                                               paste0("This are the layers of",
                                                                      " environmental change scenario.\n\n",
                                                                      "NicheToolBox uses them either",
                                                                      " to project the niche models or",
                                                                      " the PCAs ",
                                                               "computed for your niche layers.\n\n"),
                                                               paste0("Please make sure that their",
                                                                      " names are the same as the",
                                                                      " niche layers."),
                                                               br(),br(),
                                                               shinyFiles::shinyDirButton(id = paste0(
                                                                 "proj_layers_directory"),
                                                                 label =paste0("Select projection",
                                                                               " layers directory"),
                                                                 title = paste0("Select a directory",
                                                                                " from the directories panel"),
                                                                 icon = icon("folder-open",
                                                                             lib = "glyphicon"),
                                                                 buttonType = "info"),
                                                               HTML(paste('<div class="alert alert-info"',
                                                                          ' role="alert">',
                                                                          verbatimTextOutput("proj_directory"),
                                                                          '</div>')))),
                             p(paste0("If you dont have environmental",
                                      " data ntbox provides methods from",
                                      " `raster` package and other sources ("),
                               HTML('<a href="http://worldclim.org/" target="_blank"> WorldClim</a>,'),
                               HTML('<a href="http://chelsa-climate.org" target="_blank"> CHELSA</a>,'),
                               HTML('<a href="http://www.bio-oracle.org" target="_blank"> Bio-Oracle</a>,'),
                               HTML('<a href="https://envirem.github.io" target="_blank"> ENVIREM</a>'),
                               ") to get environmental data."),
                             checkboxInput("getEnvData",label = "Get environmental data",value = FALSE),
                             conditionalPanel("input.getEnvData == true",
                                              selectInput("env_data",label = "Select a source",
                                                          choices = c("WorldClim current"="wc",
                                                                      "CMIP5 future"="wc_future",
                                                                      "CHELSA current (1 km)" = "ch_pre",
                                                                      "CHELSA future (1 km)" = "ch_fut",
                                                                      "ENVIREM current" ="env_pres",
                                                                      "ENVIREM past" = "env_pass",
                                                                      "ENVIREM elevation current" ="env_elev_pres",
                                                                      "ENVIREM elevation past" ="env_elev_pass",
                                                                      "Bio-Oracle present
                                                                      (via sdmpredictors)"="bio_pre",
                                                                      "Bio-Oracle future
                                                                      (via sdmpredictors)"="bio_fut")),

                                              conditionalPanel("input.env_data=='wc_future'",
                                                               selectInput("CMIP5_mod",
                                                                           "Select a model",
                                                                           c("ACCESS1-0"= "AC",
                                                                             "BCC-CSM1-1"="BC",
                                                                             "CCSM4"="CC",
                                                                             "CESM1-CAM5-1-FV2"="CE",
                                                                             "CNRM-CM5"="CN",
                                                                             "GFDL-CM3"="GF",
                                                                             "GFDL-ESM2G"="GD",
                                                                             "GISS-E2-R"="GS",
                                                                             "HadGEM2-AO"="HD",
                                                                             "HadGEM2-CC"="HG",
                                                                             "HadGEM2-ES"="HE",
                                                                             "INMCM4"="IN",
                                                                             "IPSL-CM5A-LR"="IP",
                                                                             "MIROC-ESM-CHEM"="MI",
                                                                             "MIROC-ESM"="MR",
                                                                             "MIROC5"="MC",
                                                                             "MPI-ESM-LR"="MP",
                                                                             "MRI-CGCM3"="MG",
                                                                             "NorESM1-M"="NO"),
                                                                           selected = "CC"),
                                                               radioButtons("year_sc","Select a year scenario",
                                                                            c("2050"=50,"2070"=70)),
                                                               radioButtons("rcp","Select a RCP",
                                                                            c("rcp26"=26,"rcp45"=45,
                                                                              "rcp60"=60,"rcp85"=85),
                                                                            selected = 85)),

                                              conditionalPanel("input.env_data=='wc' ||
                                                               input.env_data=='wc_future'",
                                                               shiny::radioButtons("wc_var",
                                                                                   label = "Select an option",
                                                                                   choices = c("Bioclim"="bio",
                                                                                               "Min temperature
                                                                                               "='tmin',
                                                                                               "Max temperature
                                                                                               "='tmax',
                                                                                               "Precipitation"=
                                                                                                 'prec')),
                                                               shiny::radioButtons("wc_resol",
                                                                                   label =  "Select a resolution",
                                                                                   choices = c("10 minutes"=10,
                                                                                               "5 minutes"=5,
                                                                                               "2.5 minutes"=2.5,
                                                                                               "30 seconds"=0.5))
                                              ),
                                              conditionalPanel("input.env_data == 'ch_fut'",
                                                               radioButtons("ch_period",
                                                                            label = "Select a time period",
                                                                                   choices = c("2041-2060",
                                                                                               "2061-2080")),
                                                               radioButtons("ch_rcp",
                                                                            label="Select a RCP",
                                                                            choices=c("rcp26",
                                                                                      "rcp45",
                                                                                      "rcp85")),
                                                               selectInput("ch_model",
                                                                            label="Select a circulation model",
                                                                            choices = c('ACCESS1-0',
                                                                                        'bcc-csm1-1',
                                                                                        'BNU-ESM',
                                                                                        'CanESM2',
                                                                                        'CCSM4',
                                                                                        'CESM1-BGC',
                                                                                        'CESM1-CAM5',
                                                                                        'CMCC-CM',
                                                                                        'CMCC-CMS',
                                                                                        'CNRM-CM5',
                                                                                        'CSIRO-Mk3-6-0',
                                                                                        'FGOALS-g2',
                                                                                        'FIO-ESM',
                                                                                        'GFDL-CM3',
                                                                                        'GFDL-ESM2G',
                                                                                        'GFDL-ESM2M',
                                                                                        'GISS-E2-H',
                                                                                        'GISS-E2-H-CC',
                                                                                        'GISS-E2-R',
                                                                                        'GISS-E2-R-CC',
                                                                                        'HadGEM2-AO',
                                                                                        'HadGEM2-CC',
                                                                                        'inmcm4',
                                                                                        'IPSL-CM5A-LR',
                                                                                        'IPSL-CM5A-MR',
                                                                                        'MIROC-ESM',
                                                                                        'MIROC-ESM-CHEM',
                                                                                        'MIROC5',
                                                                                        'MPI-ESM-LR',
                                                                                        'MPI-ESM-MR',
                                                                                        'MRI-CGCM3',
                                                                                        'MRI-ESM1',
                                                                                        'NorESM1-M'))

                                                               ),
                                              conditionalPanel("input.env_data=='wc' ||
                                                               input.env_data=='wc_future'",
                                                               radioButtons('env_format',
                                                                            paste0('Select a format to write',
                                                                                   ' your layers. If you have',
                                                                                   ' set the workflow','
                                                                                   directory they will be',
                                                                                   ' saved on it; otherwise',
                                                                                   ' they will be saved in',
                                                                                   ' the working directory',
                                                                                   ' of your R session.'),
                                                                            c('.asc','.bil',
                                                                              '.tif','.nc',
                                                                              '.sdat','.img'))
                                                               ),

                                              conditionalPanel("input.env_data=='ch_pre' ||
                                                                input.env_data=='ch_fut' ||
                                                                input.env_data=='bio_pre' ||
                                                                input.env_data=='bio_fut'",
                                                               #p(paste0("Get bioclimatic data",
                                                               #          "from CHELSA")),
                                                               checkboxInput("ch_parallel",
                                                                             "Download in parallel",
                                                                             value = TRUE)
                                              ),
                                              conditionalPanel("input.env_data=='env_pres' ||
                                                               input.env_data=='env_pass' ||
                                                               input.env_data=='env_elev_pres' ||
                                                               input.env_data=='env_elev_pass'",
                                                               selectInput("env_reg","Select a region",
                                                                           choices = c("Africa",
                                                                                       "Australia",
                                                                                       "Eurasia",
                                                                                       "Europe",
                                                                                       "Global"="global",
                                                                                       "NAmerica",
                                                                                       "NewWorld",
                                                                                       "Pacific",
                                                                                       "SAmerica"),
                                                                           selected = "global"),
                                                               conditionalPanel("input.env_data=='env_pass' ||
                                                                                input.env_data=='env_elev_pass'",
                                                                                radioButtons("env_period",
                                                                                             "Select time period",
                                                                                             choices = c("Mid-Holocene"=
                                                                                                           "holo",
                                                                                                         "Last Glacial
                                                                                                         Maximum"="lgm")),
                                                                            conditionalPanel("input.env_data=='env_pass'",
                                                                                             radioButtons("env_gcm",
                                                                                                          "Global
                                                                                                          circulation
                                                                                                          model",
                                                                                                          choices = c("ccsm4",
                                                                                                                      "miroc_esm",
                                                                                                                      "mpi_esm_p")))
                                                                                ),
                                                               radioButtons("env_res",
                                                                            "Select a resolution",
                                                                            choices = c("10 arcmin"="10arcmin",
                                                                                        "5 arcmin"="5arcmin",
                                                                                        "2.5 arcmin"="2.5arcmin",
                                                                                        "30 arcsec"="30arcsec")),
                                                               radioButtons("env_fmt","Select layers format",
                                                                            choices = c("geotiff","bil"))

                                                               ),
                                              conditionalPanel("input.env_data=='bio_fut'",
                                                               radioButtons("bio_year",
                                                                            "Select a year",
                                                                            choices = c("2050",
                                                                                        "2100",
                                                                                        "2200")),
                                                               radioButtons("bio_model",
                                                                            "Select a model",
                                                                            choices = c("a")),

                                                               radioButtons("bio_scenario",
                                                                            label = "Select a scenario",
                                                                            choices = c("a")),

                                                               selectInput("bio_type_fut",
                                                                          label = "Select a variable",
                                                                          choices = c("a"))


                                              ),
                                              conditionalPanel("input.env_data=='bio_pre'",
                                                               selectInput("bio_type",
                                                                           label = "Select a variable",
                                                                           choices=c('salinity',
                                                                                     'sea_surface_temperature',
                                                                                     'current_velocity',
                                                                                     'sea_water_temperature',
                                                                                     'sea_water_salinity',
                                                                                     'sea_ice_thickness',
                                                                                     'chlorophyll_concentration',
                                                                                     'sea_surface_salinity'))
                                                               ),
                                              conditionalPanel("input.env_data=='wc' ||
                                                               input.env_data=='ch_pre' ||
                                                               input.env_data=='env_pres' ||
                                                               input.env_data=='env_elev_pres' ||
                                                               input.env_data=='bio_pre'",

                                                               bsButton("get_now","Get it",
                                                                        icon = icon("download", lib = "glyphicon"),
                                                                        style = "primary")
                                                               ),

                                              conditionalPanel("input.env_data=='wc_future' ||
                                                               input.env_data=='ch_fut' ||
                                                               input.env_data== 'env_pass' ||
                                                               input.env_data== 'env_elev_pass' ||
                                                               input.env_data== 'bio_fut'",
                                                               bsButton("get_now_future","Get it",
                                                                        icon = icon("download",
                                                                                    lib = "glyphicon"),
                                                                        style = "primary")
                                              ),
                                              br()

                             ),

                             bsButton("loadNicheLayers",
                                      "Load niche layers",
                                      icon = icon("upload",
                                                  lib = "glyphicon"),
                                      style = "primary"),
                             busyIndicator("Loading...",wait = 0),
                             br()
                             #fileInput(inputId = "worflowPath","Select a folder for your worflow"),
                             #shinyDirButton('worflowPath', 'Folder select', 'Please select a folder'),
                             #shinyDirButton("workflowPath",title = "Choose Download Directory",
                             #              label = "Browse Download Directory ", class = "btn-block btn-link"),

                             #submitButton()

                             #h4('Tutorials'),
                             #HTML('In this part you can find some tutorials that show how to use the app.'),
                             #selectInput(inputId = 'tutorialsm',label = 'Select one of the turtorials',
                             #            choices =  c('About'='about1','1. Data'='datose'),
                              #           selected = "about1",multiple = FALSE)
                               ),
                           mainPanel(
                             #htmlOutput("page"),
                             plotOutput("niche_layers"),
                             conditionalPanel("input.load_projLayers == true ||
                                              input.load_projLayers_win == true",
                                              plotOutput("niche_layers_proj")
                                              )
                             )
                           )
